/*
 * @Description:Client_UDP
 * 
 * @Author: rendc
 * 
 * @Date: 2025-08-25 14:12:05
 * 
 * @LastEditors: rendc
 * 
 * @LastEditTime: 2025-08-25 14:29:22
 */

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class TestUDPClient {

  public static void main(String[] args) {
    // UDP-Client
    String ip = "127.0.0.1";
    int port = 8888;
    // 关键的类
    DatagramSocket socket = null;
    DatagramPacket packet = null;
    try {
      // 负责发送数据
      socket = new DatagramSocket();
      // 准备要发送的数据
      byte[] buff = "hello,world".getBytes();
      packet = new DatagramPacket(buff, 0, buff.length, InetAddress.getByName(ip), port);
      // 发送数据
      socket.send(packet);
      System.out.println("客户端数据发送完成");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // 关闭资源
      if (socket != null) {
        socket.close();
      }
    }
  }
}